package com.xiaohai.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiaohai.common.R;
import com.xiaohai.projo.Admin;
import com.xiaohai.projo.Student;
import com.xiaohai.projo.Teacher;
import com.xiaohai.service.AdminService;
import com.xiaohai.service.StudentService;
import com.xiaohai.service.TeacherService;
import com.xiaohai.utils.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.java.Log;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xiaohai
 * @since 2024-04-01
 */
@Log
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private AdminService adminService;

    @GetMapping("/login")
    public R<Object> login(HttpServletRequest request, String account, String password, Integer identity) {

        String token = JwtUtil.createToken(account, password);
        HashMap<String, Object> map = new HashMap<>();
        map.put("token", token);
        // 管理员账户登录
        switch (identity) {
            case 1:
                LambdaQueryWrapper<Admin> adminLambdaQueryWrapper = new LambdaQueryWrapper<>();
                adminLambdaQueryWrapper.eq(account != null, Admin::getAccount, account).eq(password != null, Admin::getPassword, password);
                Admin admin = adminService.getOne(adminLambdaQueryWrapper);
                map.put("user", admin);
                if (!ObjectUtils.isEmpty(admin)) {
                    return R.success(map, "登录成功");
                }
                break;
            case 2:
                // 对学生账号登录判断
                LambdaQueryWrapper<Student> studentQueryWrapper = new LambdaQueryWrapper<>();
                studentQueryWrapper.eq(account != null, Student::getAccount, account).eq(password != null, Student::getPassword, password)
                        .eq(true, Student::getIsDelete, 0);
                Student student = studentService.getOne(studentQueryWrapper);
                map.put("user", student);
                if (!ObjectUtils.isEmpty(student)) {
                    return R.success(map, "登录成功");
                }
                break;
            case 3:
                // 对老师登录账号判断
                LambdaQueryWrapper<Teacher> teacherQueryWrapper = new LambdaQueryWrapper<>();
                teacherQueryWrapper.eq(account != null, Teacher::getAccount, account).eq(password != null, Teacher::getPassword, password);
                Teacher teacher = teacherService.getOne(teacherQueryWrapper);
                map.put("user", teacher);
                if (!ObjectUtils.isEmpty(teacher)) {
                    return R.success(map, "登录成功");
                }
                break;
        }
        return R.error("登录失败!，查询该账户为空", 201);
    }

    /**
     * 更新用户信息
     *
     * @param student
     * @return
     */
    @PutMapping("/update")
    public R<Object> update(@RequestBody Student student, Integer identity) {
        String account = null;
        switch (identity) {
            case 1:// 管理员
                Admin admin = new Admin();
                BeanUtils.copyProperties(student, admin);
                LambdaQueryWrapper<Admin> adminLambdaQueryWrapper = new LambdaQueryWrapper<>();
                account = admin.getAccount();
                adminLambdaQueryWrapper.eq(account != null, Admin::getAccount, account);
                adminService.update(admin, adminLambdaQueryWrapper);
                return R.success(admin, "修改成功");
            case 2:
                account = student.getAccount();
                LambdaQueryWrapper<Student> stringLambdaQueryWrapper = new LambdaQueryWrapper<>();
                stringLambdaQueryWrapper.eq(account != null, Student::getAccount, account);
                studentService.update(student, stringLambdaQueryWrapper);
                return R.success(student, "修改成功");
            case 3:
                Teacher teacher = new Teacher();
                BeanUtils.copyProperties(student, teacher);
                account = teacher.getAccount();
                LambdaQueryWrapper<Teacher> teacherLambdaQueryWrapper = new LambdaQueryWrapper<>();
                teacherLambdaQueryWrapper.eq(account != null, Teacher::getAccount, account);
                teacherService.update(teacher, teacherLambdaQueryWrapper);
                return R.success(teacher, "修改成功");
        }


        return R.error("修改失败", 201);
    }

    /**
     * 跟新密码
     *
     * @param account
     * @param newPassword
     * @return
     */
    @PutMapping("/updateByAccount")
    public R<String> updatePassword(@RequestParam("account") String account, @RequestParam("new_password") String newPassword, Integer identity) {
        boolean update = false;
        switch (identity) {
            case 1:
                update = adminService.updatePasswordByAccount(account, newPassword);
                break;
            case 2:
                update = studentService.updatePasswordByAccount(account, newPassword);
                break;
            case 3:
                update = teacherService.updatePasswordByAccount(account, newPassword);
                break;
        }
        if (update) {
            return R.success(null, "更新密码成功");
        }
        return R.error("更新密码失败", 201);
    }
}
